package com.bit.mybatis.mapper;

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface UserInfoMapper {
    @Results(id = "BaseMap" , value={
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from user_info")
    //@Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag ,create_time as createTime, update_time as updateTime from user_info;")
    List<UserInfo> selectAll();

    @ResultMap("BaseMap")
    @Select("select * from user_info where id = #{id}")
    UserInfo selectById(Integer id);//参数只有一个时，名字可以不一样，mybatis没得选

    @ResultMap("BaseMap")
    @Select("select * from user_info where id = ${id}")
    UserInfo selectById2(Integer id);

    @Select("select * from user_info where username = #{name}")
    List<UserInfo> selectByUsername(String name);
    @Select("select * from user_info where username = '${name}'")
    List<UserInfo> selectByUsername2(String name);

    @Select("select * from user_info where age = #{age} and delete_flag = #{deleteFlag}")
    List<UserInfo> selectByAge(Integer age, Integer deleteFlag);//param1,param2

    @Insert("insert into user_info(username, password,age) values (#{username},#{password},#{age})")//保证和UserInfo对象属性一样
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info(username, password,age) values (#{userInfo.username},#{userInfo.password},#{userInfo.age})")//保证和UserInfo对象属性一样
    Integer insertUser2(@Param("userInfo")UserInfo userInfo);

    @Delete("delete from user_info where id= #{id}")
    Integer deleteById(Integer id);

    @Update("update user_info set password = #{password} where id = #{id}")
    Integer updateById(UserInfo userInfo);

    @Select("select * from user_info order by age ${order}")
    List<UserInfo> selectByOrder(String order);

    @Select("select * from user_info where username like concat('%',#{name},'%')")
    List<UserInfo> selectByLike(String name);
}
